Page({
  data: {
    buildings: ['1号楼', '2号楼', '3号楼', '4号楼', '5号楼', '6号楼'],
    units: ['1单元', '2单元', '3单元', '4单元', '5单元'],
    roomNumbers: ['101', '102', '201', '202', '301', '302', '401', '402', '501', '502', '601', '602'],
    rooms: ['主卧', '次卧', '南中卧', '北卧', '厨房间', '大厅间', '大厅间大', '大厅间小'],
    selectedBuilding: '', // 新房间的楼号
    selectedUnit: '', // 新房间的单元
    selectedRoomNumber: '', // 新房间的房号
    selectedRoom: '', // 新房间的房间
    oldBuilding: '', // 旧房间的楼号
    oldUnit: '', // 旧房间的单元
    oldRoomNumber: '', // 旧房间的房号
    oldRoom: '', // 旧房间的房间
    oldRoomDueDate: '', // 旧房间的到期日
    oldRoomRentAmount: 0, // 旧房间的月租金
    remainingRent: 0 // 剩余房租
  },
  onLoad(options) {
    // 从上一页获取旧房间的信息
    this.setData({
      oldBuilding: options.building || '',
      oldUnit: options.unit || '',
      oldRoomNumber: options.roomNumber || '',
      oldRoom: options.room || '',
      oldRoomDueDate: options.dueDate || '',
      oldRoomRentAmount: Number(options.rentAmount) || 0
    });

    // 计算剩余房租
    this.calculateRemainingRent();
  },
  calculateRemainingRent() {
    const today = new Date();
    const oldDueDate = new Date(this.data.oldRoomDueDate);
    const remainingDays = Math.ceil((oldDueDate - today) / (1000 * 60 * 60 * 24));
    const remainingRent = (this.data.oldRoomRentAmount / 30) * remainingDays;

    this.setData({
      remainingRent: remainingRent.toFixed(2)
    });
  },
  buildingChange(e) {
    this.setData({
      selectedBuilding: this.data.buildings[e.detail.value]
    });
  },
  unitChange(e) {
    this.setData({
      selectedUnit: this.data.units[e.detail.value]
    });
  },
  roomNumberChange(e) {
    this.setData({
      selectedRoomNumber: this.data.roomNumbers[e.detail.value]
    });
  },
  roomChange(e) {
    this.setData({
      selectedRoom: this.data.rooms[e.detail.value]
    });
  },
  handleConfirm() {
    if (!this.data.selectedBuilding || !this.data.selectedUnit || !this.data.selectedRoomNumber || !this.data.selectedRoom) {
      wx.showToast({
        title: '请选择完整的房间信息',
        icon: 'none'
      });
      return;
    }

    // 模拟获取新房间的状态（可根据实际情况调整）
    const newRoomStatus = this.getNewRoomStatus();

    if (newRoomStatus === '闲置') {
      // 获取新房间的月租金（可根据实际情况调整）
      const newRoomRentAmount = this.getNewRoomRentAmount();

      // 计算新房间的到期日
      const today = new Date();
      const remainingDays = Math.ceil(this.data.remainingRent / (newRoomRentAmount / 30)); // 剩余房费 / (新房间月租金 / 30)
      const newDueDate = new Date(today);
      newDueDate.setDate(today.getDate() + remainingDays);

      // 跳转到新房间的详情页，并传递计算后的到期日
      wx.navigateTo({
        url: `/pages/detail/detail?building=${this.data.selectedBuilding}&unit=${this.data.selectedUnit}&roomNumber=${this.data.selectedRoomNumber}&room=${this.data.selectedRoom}&dueDate=${newDueDate.toISOString().split('T')[0]}&rentAmount=${newRoomRentAmount}`
      });
    } else {
      // 新房间状态为"在租"或"欠费"，弹窗提示
      wx.showModal({
        title: '提示',
        content: '新房间目前有人入住，请联系管理员18661701528',
        showCancel: false
      });
    }
  },
  getNewRoomStatus() {
    // 模拟获取新房间的状态（可根据实际情况调整）
    // 这里假设新房间的状态为"闲置"
    return '闲置';
  },
  getNewRoomRentAmount() {
    // 模拟获取新房间的月租金（可根据实际情况调整）
    // 这里假设新房间的月租金为 2500
    return 2500;
  }
}); 